package com.mdd.admin.service.pay.impl;

import com.github.binarywang.wxpay.service.WxPayService;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcClient;
import com.icbc.api.IcbcConstants;
import com.icbc.api.request.CardbusinessAggregatepayB2cOnlineMerrefundRequestV1;
import com.icbc.api.response.CardbusinessAggregatepayB2cOnlineMerrefundResponseV1;
import com.mdd.admin.constant.YmlConstant;
import com.mdd.admin.service.order.IOrderService;
import com.mdd.admin.service.pay.ICBCService;
import com.mdd.common.entity.order.Order;
import com.mdd.common.entity.refund.OrderRefund;
import com.mdd.common.mapper.order.OrderMapper;
import com.mdd.common.mapper.recharge.RechargeOrderMapper;
import com.mdd.common.mapper.refund.OrderRefundMapper;
import com.mdd.common.mapper.user.UserAuthMapper;
import com.mdd.common.utils.SnUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Slf4j
@Data
public class ICBCServiceImpl implements ICBCService {

	@Resource
	private IcbcClient client;

	@Resource
	private IOrderService iOrderService;

	@Resource
	private RechargeOrderMapper rechargeOrderMapper;

	@Resource
	private UserAuthMapper userAuthMapper;

	@Resource(name = "mpWxPayService")
	private WxPayService wxPayService;

	@Resource
	private YmlConstant ymlConstant;

	@Resource
	private OrderRefundMapper orderRefundMapper;

	@Resource
	private OrderMapper orderMapper;


	@Override
	public void refund(OrderRefund order, Long userId) {
		//签名类型为RSA2时，需传入appid，私钥和网关公钥，签名类型使用定值IcbcConstants.SIGN_TYPE_RSA2，其他参数使用缺省值
		DefaultIcbcClient client = new DefaultIcbcClient(ymlConstant.appId, IcbcConstants.SIGN_TYPE_RSA2, ymlConstant.icbcPrivateKey, ymlConstant.icbcPublicKey);
		CardbusinessAggregatepayB2cOnlineMerrefundRequestV1 request = new CardbusinessAggregatepayB2cOnlineMerrefundRequestV1();
		//根据测试环境和生产环境替换相应ip和端口
		request.setServiceUrl(ymlConstant.serviceUrl + ymlConstant.icbcRefundqry);
		//请对照接口文档用bizContent.setxxx()方法对业务上送数据进行赋值
		CardbusinessAggregatepayB2cOnlineMerrefundRequestV1.CardbusinessAggregatepayB2cOnlineMerrefundRequestV1Biz bizContent = new CardbusinessAggregatepayB2cOnlineMerrefundRequestV1.CardbusinessAggregatepayB2cOnlineMerrefundRequestV1Biz();
		request.setBizContent(bizContent);
		bizContent.setOrder_id(order.getTransactionId());//消费工行订单号,工行订单号，商户订单号或行内订单号必须其中一个不为空
		bizContent.setMer_id(ymlConstant.merId);//商户编号-必输项
		//bizContent.setOut_trade_no(order.getSn());//商户订单号,工行订单号，商户订单号或行内订单号必须其中一个不为空
		String sn = SnUtils.getRefundOrderSn();
		bizContent.setOuttrx_serial_no(sn);//外部退货流水号-必输项
		bizContent.setRet_total_amt(order.getOrderAmount().multiply(new BigDecimal(100)).intValue()+"");//退货总金额-必输项
		bizContent.setTrnsc_ccy("001");//交易币种-必输项
		bizContent.setOrder_apd_inf("");
		bizContent.setIcbc_appid(ymlConstant.appId);
		CardbusinessAggregatepayB2cOnlineMerrefundResponseV1 response;
		try {
			log.info("开始调用ICBC退款接口请求参数是: {}", bizContent);
			response = client.execute(request, System.currentTimeMillis() + "");//msgId消息通讯唯一编号，要求每次调用独立生成，APP级唯一
			log.info("调用ICBC退款接口成功 ");
			if (response.isSuccess()) {
				// 业务成功处理，请根据接口文档用response.getxxx()获取同步返回的业务数据
				log.info("退款成功： ReturnCode:{}", response.getReturnCode());
				log.info("退款成功： response:{}", response);
			} else {
				// 失败
				log.error("退款失败：ReturnCode:{}", response.getReturnCode());
				log.error("退款失败: ReturnMsg:{}", response.getReturnMsg());
			}
		} catch (Exception e) {
			log.error("调用退款接口失败： {}" , e);
			e.printStackTrace();
		}
	}


}
